package com.xhwl.centiotdata.service.integrateconfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.dto.haikanginfo.InfoIntegrationPropertiesDTO;
import com.xhwl.common.enums.CategoryEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author： wuyuhao
 * @Description：
 * @Date 2022/7/29
 */
@Component
@Slf4j
public class InfoIntegrationConfig extends IntegrationConfig<Object, List<InfoIntegrationPropertiesDTO>>{

    private List<InfoIntegrationPropertiesDTO> infoIntegrationPropertiesDTOList = new ArrayList<>();

    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handleInfoIntegrationConfig();
    }

    @Override
    public List<InfoIntegrationPropertiesDTO> getIntegrationConfig(Object o) {
        return infoIntegrationPropertiesDTOList;
    }

    private void handleInfoIntegrationConfig() {
        infoIntegrationPropertiesDTOList = IntegrationConfig.integrationConfigList.stream()
                .filter(deviceProjSupplierCategory ->
                    CategoryEnum.INFO_SYSTEM.getId().equals(deviceProjSupplierCategory.getProdCategoryId())
                            && deviceProjSupplierCategory.getProperties() != null)
                .map(deviceProjSupplierCategory -> {
                    log.info("deviceProjSupplierCategory: {}", deviceProjSupplierCategory);
                    JSONObject properties = deviceProjSupplierCategory.getProperties();
                    InfoIntegrationPropertiesDTO propertiesDTO = JSON.toJavaObject(properties, InfoIntegrationPropertiesDTO.class);
                    propertiesDTO.setProjectId(deviceProjSupplierCategory.getProjectId());
                    propertiesDTO.setProductId(deviceProjSupplierCategory.getProductId());
                    propertiesDTO.setCategoryEnum(CategoryEnum.INFO_SYSTEM);
                    return propertiesDTO;
                })
                .filter(propertiesDTO -> propertiesDTO.getIpAndPort() != null
                        && propertiesDTO.getAppKey() != null
                        && propertiesDTO.getAppSecret() != null)
                .collect(Collectors.toList());
    }
}
